using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QiHe.CodeLib;

namespace QiHe.ReliableData
{
    public partial class TypeEditorDialog : Form
    {
        DataGridViewHelper<TypeField> gridViewHelper;
        public TypeEditorDialog()
        {
            InitializeComponent();
            gridViewHelper = new DataGridViewHelper<TypeField>(dataGridViewFields);
        }

        public Dictionary<string, DataType> DataTypes = new Dictionary<string, DataType>();


        public void SetDataTypes(List<DataType> dataTypes)
        {
            DataTypes.Clear();
            foreach (DataType dataType in dataTypes)
            {
                DataTypes.Add(dataType.Name, dataType);
            }
            FileComboBoxTypes();
        }

        private void FileComboBoxTypes()
        {
            toolStripComboBoxTypes.Items.Clear();
            foreach (string name in DataTypes.Keys)
            {
                toolStripComboBoxTypes.Items.Add(name);
            }
            toolStripComboBoxTypes.Sorted = true;

            List<string> types = new List<string>(Package.Types.Keys);
            types.Sort(StringComparer.Ordinal);
            this.FieldType.Items.Clear();
            this.FieldType.Items.AddRange(types.ToArray());
        }

        DataType CurrentDataType;

        private void UpdateCurrentDataType()
        {
            if (CurrentDataType == null) return;
            dataGridViewFields.EndEdit();
            CurrentDataType.Fields.Clear();
            CurrentDataType.Fields.AddRange(gridViewHelper.GetDataList());
        }

        private void toolStripButtonNewType_Click(object sender, EventArgs e)
        {
            string typeName = toolStripComboBoxTypes.Text;
            if (typeName == null) return;
            if (DataTypes.ContainsKey(typeName))
            {
                MessageBox.Show(typeName + " already exists.");
            }
            else
            {
                CreateNewDataType(typeName);
                dataGridViewFields.Rows.Clear();
            }
        }

        private void toolStripButtonCopy_Click(object sender, EventArgs e)
        {
            string typeName = toolStripComboBoxTypes.Text;
            if (typeName == null) return;
            if (DataTypes.ContainsKey(typeName))
            {
                MessageBox.Show(typeName + " already exists.");
            }
            else
            {
                CreateNewDataType(typeName);
            }

        }

        private void CreateNewDataType(string typeName)
        {
            UpdateCurrentDataType();
            CurrentDataType = new DataType(typeName);
            DataTypes.Add(typeName, CurrentDataType);
            toolStripComboBoxTypes.Items.Add(typeName);
            this.FieldType.Items.Add(typeName);
            this.FieldType.Items.Add(TypeHelper.MakeListType(typeName));
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            string typeName = toolStripComboBoxTypes.Text;
            if (DataTypes.ContainsKey(typeName))
            {
                DataTypes.Remove(typeName);
                toolStripComboBoxTypes.Items.Remove(typeName);
            }
        }

        private void toolStripComboBoxTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateCurrentDataType();
            CurrentDataType = DataTypes[toolStripComboBoxTypes.SelectedItem.ToString()];
            gridViewHelper.SetDataList(CurrentDataType.Fields);
        }

        private void TypeEditorDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            UpdateCurrentDataType();
        }

        private void deleteRowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridViewHelper.DeleteSelectedRows();
        }

        private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridViewHelper.MoveSelectedRowsUp();
        }

        private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridViewHelper.MoveSelectedRowsDown();
        }
    }
}